<?php

class Photo extends MX_Controller {

	public function __construct()
	{
		parent::__construct();
		
		$this->load->helper(array('form', 'url'));
		
		$this->load->model('photo_model');
		
		$this->lang->load('photo/photo', 'english');
	}
	
	/*
	 * Albums
	 */
	public function index()
	{
		if (!$this->session->userdata('session_loggedin')) {
			redirect('user/login/' . $this->core->encode($this->uri->uri_string()));
		}
		
		$data['title'] = $this->lang->line('title');
		$data['heading_title'] = $this->lang->line('album_heading_title');
		$data['content'] = 'index_view';
		
		$results = $this->photo_model->getAlbums();
		
		$data['albums'] = array();
		
		foreach ($results as $result) {
			$data['albums'][] = array(
				'name'	=> anchor('photo/photos/'.$result['id'], $result['name']),
				'desc'	=> $result['description'],
				'count'	=> $this->photo_model->countPhoto($result['id']),
				'edit'	=> anchor('photo/edit_album/'.$result['id'], 'Edit')
			);
		}
		
		$this->load->view('template', $data);
	}
	
	/*
	 * Add album
	 */
	public function add_album()
	{
		if (!$this->session->userdata('session_loggedin')) {
			redirect('user/login/' . $this->core->encode($this->uri->uri_string()));
		}
		
		$this->load->library('form_validation');
		
		$data['title'] = $this->lang->line('title');
		$data['heading_title'] = $this->lang->line('add_album_heading_title');
		$data['content'] = 'add_album';
		
		$this->form_validation->set_rules('name', 'Name', 'required|is_unique[photo_album.name]');
		
		if ($this->form_validation->run() == TRUE) {
			$this->photo_model->insertAlbum(array(
				'name' 			=> $this->input->post('name'),
				'description'	=> $this->input->post('desc')
			));
			
			redirect('photo');
		}
		
		$this->load->view('template', $data);
	}
	
	/*
	 * Edit album
	 */
	public function edit_album()
	{
		if (!$this->session->userdata('session_loggedin')) {
			redirect('user/login/' . $this->core->encode($this->uri->uri_string()));
		}
		
		$this->load->library('form_validation');
		
		$data['title'] = $this->lang->line('title');
		$data['heading_title'] = $this->lang->line('edit_album_heading_title');
		$data['content'] = 'edit_album';
		
		$album_id = $this->uri->segment(3);
		
		$result = $this->photo_model->getAlbum($album_id);
		$data['album'] = $result;
		
		//$this->form_validation->set_rules('name', 'Name', 'required|is_unique[photo_album.name]');
		
		if ($this->input->post('name')) {
			$this->photo_model->updateAlbum(array(
				'name' 			=> $this->input->post('name'),
				'description'	=> $this->input->post('desc')
			), $album_id);
			
			redirect('photo');
		}
		
		$this->load->view('template', $data);
	}
	
	/*
	 * Photos of Album
	 */
	public function photos()
	{
		if (!$this->session->userdata('session_loggedin')) {
			redirect('user/login/' . $this->core->encode($this->uri->uri_string()));
		}
		
		$album_id = $this->uri->segment(3);
		if (!$album_id) {
			show_404();
		}
		
		$limit = 12;		
		$offset = $this->uri->segment(4);
		
		$results = $this->photo_model->getAll($limit, $offset, $album_id);
		//print_r($results);
		$data['images'] = array();
		
		foreach ($results as $result) {
			$data['images'][] = array(
				'id'			=> $result['id'],
				'name'			=> $result['name'],
				'source'		=> $result['source'],
				'desc'			=> $result['description'],
				'title_full'	=> '<p>Title:<br />'.$result['name'].'</p><p>Souce:<br />'.$result['source'].'</p><p>Description:<br />'.$result['description'].'</p>',
				'thumb'			=> $this->config->base_url('photo/image/' . $result['id']),
				'edit'			=> array(
					'title'		=> 'Edit',
					'href'		=> base_url('photo/edit/'.$result['id'].'/'.$this->uri->segment(3).'/'.$this->uri->segment(4))
				),
				'delete'		=> array(
					'title'		=> 'Delete',
					'href'		=> base_url('photo/delete_photo/'.$result['id'].'/'.$this->uri->segment(3).'/'.$this->uri->segment(4))
				)
			);
		}
		//echo '<pre>';print_r($data['images']);echo '</pre>';
		
		$this->load->library('pagination');
		
		$config = array(
			'base_url'			=> $this->config->base_url('photo/photos/'.$album_id),
			'total_rows'		=> $this->photo_model->getTotalRows($album_id),
			'per_page'			=> $limit,
			'uri_segment'		=> 4,
			//'use_page_numbers'	=> TRUE
		);
		
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		$data['total'] = $this->photo_model->getTotalRows($album_id);
		
		$data['title'] = $this->lang->line('title');
		$data['content'] = 'photos_view';
		
		$this->load->view('template', $data);
	}
	
	/*
	 * Add photo to album
	 */
	public function add()
	{
		if (!$this->session->userdata('session_loggedin')) {
			redirect('user/login/' . $this->core->encode($this->uri->uri_string()));
		}
		
		$album_id = $this->uri->segment(3);
		
		$this->load->library('form_validation');
		
		$config = array(
			'upload_path'	=> FCPATH . 'data/temp/',
			'allowed_types'	=> 'jpg|png|gif'
		);
	
		$this->load->library('upload', $config);
		
		$this->form_validation->set_rules('name', 'Name', 'required');
		//$this->form_validation->set_rules('file', 'File', 'required');
		$data['error'] = '';
		
		if ($this->form_validation->run()) {
			if ($this->upload->do_upload('file')) {
				$data = array('upload_data' => $this->upload->data());
				$fullPath = $data['upload_data']['full_path'];
				$fileType = $data['upload_data']['file_type'];
				
				$image = file_get_contents($fullPath);
				
				$data = array(
					'album_id'		=> $album_id,
					'name'			=> $this->input->post('name'),
					'source'		=> $this->input->post('source'),
					'photo'			=> $image,
					'photo_type'	=> $fileType,
					'description'	=> $this->input->post('description'),
					'time_uploaded'	=> date('Y-m-d H:i:s')
				);
				
				$this->photo_model->insert($data);
				
				@unlink($fullPath);
				
				redirect('photo/photos/'.$album_id);
			} else {
				$data['error'] = $this->upload->display_errors();
			}
		}	
		
		$data['title'] = $this->lang->line('title');
		$data['content'] = 'add_view';
		
		$this->load->view('template', $data);
	}
	
	/*
	 * Edit photo
	 */
	public function edit()
	{
		if (!$this->session->userdata('session_loggedin')) {
			redirect('user/login/' . $this->core->encode($this->uri->uri_string()));
		}
		
		$photo_id = $this->uri->segment(3);
		$album_id = $this->uri->segment(4);
		$page_offset = $this->uri->segment(5);
		
		if (!$photo_id OR !$album_id) {
			show_404();
		}
		
		$data['title'] = $this->lang->line('title');
		$data['content'] = 'edit_view';
		
		$result = $this->photo_model->getPhoto($photo_id);
		
		$data['photo'] = $result;
		
		$albums = $this->photo_model->getAlbums();
		
		$data['album'] = array();
		$data['album_selected'] = '';
		
		foreach ($albums as $album) {
			$data['album'][$album['id']] = $album['name'];
			
			if ($album['id'] == $result['album_id']) {
				$data['album_selected'] = $album['id'];
			}
		}
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('name', 'Name', 'required');
		
		if ($this->form_validation->run()) {
			$data = array(
				'album_id'		=> $this->input->post('album'),
				'name'			=> $this->input->post('name'),
				'source'		=> $this->input->post('source'),
				'description'	=> $this->input->post('description'),
				'time_edited'	=> date('Y-m-d H:i:s')
			);
			
			$this->photo_model->update($data, $photo_id);
			
			redirect('photo/photos/'.$album_id.'/'.$page_offset);
		}
		
		$this->load->view('template', $data);
	}
	
	/*
	 * Make image from database
	 */
	public function image()
	{
		$id = $this->uri->segment(3);
		
		$result = $this->photo_model->getImage($id);
		
		header('Content-type: ' . $result['photo_type']);
		header('Cache-Control: no-cache, must-revalidate'); 
		echo $result['photo'];
		exit;
	}
	
	public function delete_photo()
	{
		if (!$this->session->userdata('session_loggedin')) {
			redirect('user/login');
		}
		
		$pid = $this->uri->segment(3);
		$album_id = $this->uri->segment(4);
		
		if (!$pid) {
			show_404();
		}
		
		$this->photo_model->deleteSelectedPhoto($pid);
		
		redirect('photo/photos/'.$album_id);
	}
}